package cn.ekuma.util;

import com.openbravo.pos.util.StringUtils;

public class MoneyUtil {
	
	/*
	 * 外部
	 * 引用页:http://www.oschina.net/code/snippet_137848_4896
	 */
	public static String uppercaseFromatRMB(double n){  
       String fraction[] = {"角", "分"};  
       String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };  
       String unit[][] = {{"元", "万", "亿"},  
                      {"", "拾", "佰", "仟"}};  
       String head = n < 0? "负": "";  
       n = Math.abs(n);  
       String s = "";  
        for (int i = 0; i < fraction.length; i++) {  
            s += (digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");  
        }  

        if(s.length()<1){  
             s = "整";      
        }  
       int integerPart = (int)Math.floor(n);  
       for (int i = 0; i < unit[0].length && integerPart > 0; i++) {  
          String p ="";  
           for (int j = 0; j < unit[1].length && n > 0; j++) {  
                 p = digit[integerPart%10]+unit[1][j] + p;  
                 integerPart = integerPart/10;  
            }  
           s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;  
       }  
      return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");  
     }
	
	/*
	 *
	 * 全额打印式
	 */
	public static String uppercaseFullFromatRMB(double n){  
       String fraction[] = {"角", "分"};  
       String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };  
       String unit[][] = {{"元", "万", "亿"},  
                      {"", "拾", "佰", "仟"}};  
       String head = n < 0? "负": "";  
       n = Math.abs(n);  
       if(n==0)
    	   return "零元整";
       String s = "";  
        for (int i = 0; i < fraction.length; i++) {  
            s += digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i];  
        }  
        if(s.length()<1){  
             s = "整";      
        }  
       int integerPart = (int)Math.floor(n);  
       for (int i = 0; i < unit[0].length && integerPart > 0; i++) {  
          String p ="";  
           for (int j = 0; j < unit[1].length && n > 0; j++) {  
                 p = digit[integerPart%10]+unit[1][j] + p;  
                 integerPart = integerPart/10;  
            }  
           s = p+ unit[0][i] + s;  
       }  
      return head + s.replaceFirst("(零.)+", "");  
     }
	
	/*
	 * 填空式
	 * 返回一数据从负，分，角，元。。。。
	 */
	public static String[] fillsUpFromatRMB(double n){  
	       String fraction[] = {"角", "分"};  
	       String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };  
	       String unit[][] = {{"元", "万", "亿"},  
	                      {"", "拾", "佰", "仟"}}; 
	       String head = n < 0? "负": "正";  
	       n = Math.abs(n);
	       int integerPart = (int)Math.floor(n);  
	       String[] retString=new String[String.valueOf(integerPart).length()+fraction.length+1];
	       if(integerPart==0&&n!=0)
	    	   retString=new String[fraction.length+1];
	       retString[0]=head;  
	       for (int i = 0; i < fraction.length; i++) {  
	        	retString[fraction.length-i]= digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)];  
	        }
	       if(n==0)
	        	retString[fraction.length+1]="零";
	       else
	         for (int i = 0; i < unit[0].length && integerPart > 0; i++) {  
	           for (int j = 1; j <= unit[1].length && integerPart > 0; j++) {  
	        	     int arr=i*unit[1].length+j+fraction.length;
	        	     retString[arr] = digit[integerPart%10];  
	                 integerPart = integerPart/10;
	            }
	         }  
	      return retString;  
	}
	
	
	 public static void main(String agrs[]) {  
		 //testUppercaseFromatRMB();
		 testUppercaseFullFromatRMB();
		 //testFillsUpFromatRMB();
  }  
	 
	 
	 
	private static void testUppercaseFromatRMB() {
		// 整数
		System.out.println(uppercaseFromatRMB(0)); // 零元整
		System.out.println(uppercaseFromatRMB(123)); // 壹佰贰拾叁元整
		System.out.println(uppercaseFromatRMB(1000000)); // 壹佰万元整
		System.out.println(uppercaseFromatRMB(100000001)); // 壹亿零壹元整
		System.out.println(uppercaseFromatRMB(1000000000)); // 壹拾亿元整
		System.out.println(uppercaseFromatRMB(1234567890)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元整
		System.out.println(uppercaseFromatRMB(1001100101)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFromatRMB(110101010)); // 壹亿壹仟零壹拾万壹仟零壹拾元整

		// 小数
		System.out.println(uppercaseFromatRMB(0.12)); // 壹角贰分
		System.out.println(uppercaseFromatRMB(123.34)); // 壹佰贰拾叁元叁角肆分
		System.out.println(uppercaseFromatRMB(1000000.56)); // 壹佰万元伍角陆分
		System.out.println(uppercaseFromatRMB(100000001.78)); // 壹亿零壹元柒角捌分
		System.out.println(uppercaseFromatRMB(1000000000.90)); // 壹拾亿元玖角
		System.out.println(uppercaseFromatRMB(1234567890.03)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(uppercaseFromatRMB(1001100101.00)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFromatRMB(110101010.10)); // 壹亿壹仟零壹拾万壹仟零壹拾元壹角

		// 负数
		System.out.println(uppercaseFromatRMB(-0.12)); // 负壹角贰分
		System.out.println(uppercaseFromatRMB(-123.34)); // 负壹佰贰拾叁元叁角肆分
		System.out.println(uppercaseFromatRMB(-1000000.56)); // 负壹佰万元伍角陆分
		System.out.println(uppercaseFromatRMB(-100000001.78)); // 负壹亿零壹元柒角捌分
		System.out.println(uppercaseFromatRMB(-1000000000.90)); // 负壹拾亿元玖角
		System.out.println(uppercaseFromatRMB(-1234567890.03)); // 负壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(uppercaseFromatRMB(-1001100101.00)); // 负壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFromatRMB(-110101010.10)); // 负壹亿壹仟零壹拾万壹仟零壹拾元壹角
	}

	private static void testUppercaseFullFromatRMB() {
		// 整数
		System.out.println(uppercaseFullFromatRMB(0)); // 零元整
		System.out.println(uppercaseFullFromatRMB(123)); // 壹佰贰拾叁元整
		System.out.println(uppercaseFullFromatRMB(1000000)); // 壹佰万元整
		System.out.println(uppercaseFullFromatRMB(100000001)); // 壹亿零壹元整
		System.out.println(uppercaseFullFromatRMB(1000000000)); // 壹拾亿元整
		System.out.println(uppercaseFullFromatRMB(1234567890)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元整
		System.out.println(uppercaseFullFromatRMB(1001100101)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFullFromatRMB(110101010)); // 壹亿壹仟零壹拾万壹仟零壹拾元整

		// 小数
		System.out.println(uppercaseFullFromatRMB(0.12)); // 壹角贰分
		System.out.println(uppercaseFullFromatRMB(123.34)); // 壹佰贰拾叁元叁角肆分
		System.out.println(uppercaseFullFromatRMB(1000000.56)); // 壹佰万元伍角陆分
		System.out.println(uppercaseFullFromatRMB(100000001.78)); // 壹亿零壹元柒角捌分
		System.out.println(uppercaseFullFromatRMB(1000000000.90)); // 壹拾亿元玖角
		System.out.println(uppercaseFullFromatRMB(1234567890.03)); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(uppercaseFullFromatRMB(1001100101.00)); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFullFromatRMB(110101010.10)); // 壹亿壹仟零壹拾万壹仟零壹拾元壹角

		// 负数
		System.out.println(uppercaseFullFromatRMB(-0.12)); // 负壹角贰分
		System.out.println(uppercaseFullFromatRMB(-123.34)); // 负壹佰贰拾叁元叁角肆分
		System.out.println(uppercaseFullFromatRMB(-1000000.56)); // 负壹佰万元伍角陆分
		System.out.println(uppercaseFullFromatRMB(-100000001.78)); // 负壹亿零壹元柒角捌分
		System.out.println(uppercaseFullFromatRMB(-1000000000.90)); // 负壹拾亿元玖角
		System.out.println(uppercaseFullFromatRMB(-1234567890.03)); // 负壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(uppercaseFullFromatRMB(-1001100101.00)); // 负壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(uppercaseFullFromatRMB(-110101010.10)); // 负壹亿壹仟零壹拾万壹仟零壹拾元壹角
	}
	
	private static void testFillsUpFromatRMB() {
		// 整数
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(0),"-")); // 零元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(123),"-")); // 壹佰贰拾叁元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1000000),"-")); // 壹佰万元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(100000001),"-")); // 壹亿零壹元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1000000000),"-")); // 壹拾亿元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1234567890),"-")); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1001100101),"-")); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(110101010),"-")); // 壹亿壹仟零壹拾万壹仟零壹拾元整

		// 小数
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(0.12),"-")); // 壹角贰分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(123.34),"-")); // 壹佰贰拾叁元叁角肆分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1000000.56),"-")); // 壹佰万元伍角陆分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(100000001.78),"-")); // 壹亿零壹元柒角捌分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1000000000.90),"-")); // 壹拾亿元玖角
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1234567890.03),"-")); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(1001100101.00),"-")); // 壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(110101010.10),"-")); // 壹亿壹仟零壹拾万壹仟零壹拾元壹角

		// 负数
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-0.12),"-")); // 负壹角贰分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-123.34),"-")); // 负壹佰贰拾叁元叁角肆分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-1000000.56),"-")); // 负壹佰万元伍角陆分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-100000001.78),"-")); // 负壹亿零壹元柒角捌分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-1000000000.90),"-")); // 负壹拾亿元玖角
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-1234567890.03),"-")); // 负壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾元叁分
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-1001100101.00),"-")); // 负壹拾亿零壹佰壹拾万零壹佰零壹元整
		System.out.println(StringUtils.stringArrayConcat(fillsUpFromatRMB(-110101010.10),"-")); // 负壹亿壹仟零壹拾万壹仟零壹拾元壹角
	}
	
	
}
